package com.zl.tby.vo;

import com.zl.tby.model.TbyWildCamera;
import lombok.Data;
import org.noear.solon.validation.annotation.Length;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.wood.utils.StringUtils;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 野保相机信息
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:12
 */
@Data
public class TbyWildCameraForm {
    /**主键id*/
    private Long id;
    /**设备名称*/
    @NotBlank(message = "设备名称不能为空")
    @Length(max = 20, message = "设备名称最大长度为20")
    private String name;
    /**设备编码*/
    @NotBlank(message = "设备编码不能为空")
    @Length(max = 20, message = "设备编码最大长度为20")
    private String code;
    /**经度*/
    @NotBlank(message = "经度不能为空")
    @Length(max = 32, message = "经度最大长度为32")
    private String longitude;
    /**纬度*/
    @NotBlank(message = "纬度不能为空")
    @Length(max = 32, message = "纬度最大长度为32")
    private String latitude;
    /**所属部门*/
    private String deptId;
    /**设备类型*/
    @Length(max = 255, message = "设备类型最大长度为255")
    private String category;
    /**设备品牌*/
    @Length(max = 255, message = "设备品牌最大长度为255")
    private String brand;
    /**ip*/
    @Length(max = 255, message = "ip最大长度为255")
    private String ip;
    /**注册信息*/
    @Length(max = 255, message = "注册信息最大长度为255")
    private String registerInfo;
    /**最后拍摄时间*/
    private String shotDate;
    /**最后心跳时间*/
    private String pitpatDate;
    /**电量*/
    private BigDecimal powerLevel;
    /**在线状态 0：离线 1：在线*/
    private Integer online;

    public TbyWildCamera createEntity() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        TbyWildCamera entity = new TbyWildCamera();
        entity.setName(this.getName());
        entity.setCode(this.getCode());
        entity.setLongitude(this.getLongitude());
        entity.setLatitude(this.getLatitude());
        entity.setDeptId(this.getDeptId());
        entity.setCategory(this.getCategory());
        entity.setBrand(this.getBrand());
        entity.setRegisterInfo(this.getRegisterInfo());
        if (StringUtils.isNotEmpty(this.getShotDate())) {
            entity.setShotDate(LocalDateTime.parse(this.getShotDate(), formatter));
        }
        if (StringUtils.isNotEmpty(this.getPitpatDate())) {
            entity.setPitpatDate(LocalDateTime.parse(this.getPitpatDate(), formatter));
        }
        entity.setPowerLevel(this.getPowerLevel());
        entity.setIp(this.getIp());
        entity.setOnline(this.getOnline());
        return entity;
    }

    public void updateEntity(TbyWildCamera entity) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        entity.setName(this.getName());
        entity.setCode(this.getCode());
        entity.setLongitude(this.getLongitude());
        entity.setLatitude(this.getLatitude());
        entity.setDeptId(this.getDeptId());
        entity.setCategory(this.getCategory());
        entity.setBrand(this.getBrand());
        entity.setRegisterInfo(this.getRegisterInfo());
        if (StringUtils.isNotEmpty(this.getShotDate())) {
            entity.setShotDate(LocalDateTime.parse(this.getShotDate(), formatter));
        }
        if (StringUtils.isNotEmpty(this.getPitpatDate())) {
            entity.setPitpatDate(LocalDateTime.parse(this.getPitpatDate(), formatter));
        }
        entity.setPowerLevel(this.getPowerLevel());
        entity.setIp(this.getIp());
        entity.setOnline(this.getOnline());
    }

    public static TbyWildCameraForm convert(TbyWildCamera entity) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        TbyWildCameraForm form = new TbyWildCameraForm();
        form.setName(entity.getName());
        form.setCode(entity.getCode());
        form.setLongitude(entity.getLongitude());
        form.setLatitude(entity.getLatitude());
        form.setDeptId(entity.getDeptId());
        form.setCategory(entity.getCategory());
        form.setBrand(entity.getBrand());
        form.setRegisterInfo(entity.getRegisterInfo());
        if (entity.getShotDate() != null) {
            form.setShotDate(entity.getShotDate().format(formatter));
        }
        if (entity.getPitpatDate() != null) {
            form.setPitpatDate(entity.getPitpatDate().format(formatter));
        }
        form.setPowerLevel(entity.getPowerLevel());
        form.setIp(entity.getIp());
        form.setOnline(entity.getOnline());
        return form;
    }
}
